﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace Hi_Baidu.ProcessModel
{
    class GetInfomationModel : Model
    {
        private static byte[] PackageBody;
        private static byte[] protocol;

        /// <summary>
        ///     向服务器发送获取自身信息请求
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        public static void GetUserInfo(byte[] username)
        {
            Offset = 0;
            if (username.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + None.Length + IPAddress.Length + 12];

                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("GetMySelfInfo",
                                                                       (int) Packet.Protocol.OUT_NetworkCommand.
                                                                                 GET_MYSELF_INFO_PACKAGE,
                                                                       (int) Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     向服务器发送获取分组信息请求
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        public static void GetUserGroupInfo(byte[] username)
        {
            Offset = 0;
            if (username.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length  + None.Length + IPAddress.Length + 12];

                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("GetGroupInfo",
                                                                       (int)
                                                                       Packet.Protocol.OUT_NetworkCommand.
                                                                           GET_GROUP_PACKAGE,
                                                                       (int) Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     向服务器发送获取指定分组的好友请求
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="ItemId" type="string">
        ///     <para>
        ///         分组Id
        ///     </para>
        /// </param>
        public static void GetUserFriendByItemId(byte[] username, byte[] ItemId)
        {
            if (username.Length > 0 && ItemId.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + None.Length + IPAddress.Length + 16];
                Offset = 0;
                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //分组ID
                Array.ConstrainedCopy(ItemId, 0, PackageBody, Offset, ItemId.Length);
                Offset += 4;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("GetFriendByItem",
                                                                       (int)
                                                                       Packet.Protocol.OUT_NetworkCommand.
                                                                           GET_FRIND_BY_GROUP_PACKAGE,
                                                                       (int) Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     向服务器发送获取指定好友详细信息请求
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="FriendId" type="string">
        ///     <para>
        ///         好友ID
        ///     </para>
        /// </param>
        public static void GetFriendInfo(byte[] username, byte[] FriendId)
        {
            if (username.Length > 0 && FriendId.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + None.Length + IPAddress.Length + 16];
                Offset = 0;
                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //好友ID
                Array.ConstrainedCopy(FriendId, 0, PackageBody, Offset, FriendId.Length);
                Offset += 4;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("GetFriendInfo",
                                                                       (int)
                                                                       Packet.Protocol.OUT_NetworkCommand.
                                                                           GET_FRIEND_INFO,
                                                                       (int) Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }
    }
}
